home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / advanced97 / underwater.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  5.3 KB  |  221 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <GL/glut.h>
  5. #include "texture.h"
  6.  
  7. /* Some <math.h> files do not define M_PI... */
  8. #ifndef M_PI
  9. #define M_PI 3.14159265358979323846
  10. #endif
  11.  
  12. #ifndef __sgi
  13. /* Most math.h's do not define float versions of the math functions. */
  14. #define expf(x) ((float)exp((x)))
  15. #define sinf(x) ((float)sin((x)))
  16. #endif
  17.  
  18. static float transx = 1.0, transy, rotx, roty;
  19. static int ox = -1, oy = -1;
  20. static int mot = 0;
  21. #define PAN    1
  22. #define ROT    2
  23.  
  24. void
  25. pan(const int x, const int y) {
  26.     transx +=  (x-ox)/5.;
  27.     transy -= (y-oy)/5.;
  28.     ox = x; oy = y;
  29.     glutPostRedisplay();
  30. }
  31.  
  32. void
  33. rotate(const int x, const int y) {
  34.     rotx += x-ox;
  35.     if (rotx > 360.) rotx -= 360.;
  36.     else if (rotx < -360.) rotx += 360.;
  37.     roty += y-oy;
  38.     if (roty > 360.) roty -= 360.;
  39.     else if (roty < -360.) roty += 360.;
  40.     ox = x; oy = y;
  41.     glutPostRedisplay();
  42. }
  43.  
  44. void
  45. motion(int x, int y) {
  46.     if (mot == PAN) pan(x, y);
  47.     else if (mot == ROT) rotate(x,y);
  48. }
  49.  
  50. void
  51. mouse(int button, int state, int x, int y) {
  52.     if(state == GLUT_DOWN) {
  53.     switch(button) {
  54.     case GLUT_LEFT_BUTTON:
  55.         mot = PAN;
  56.         motion(ox = x, oy = y);
  57.         break;
  58.     case GLUT_RIGHT_BUTTON:
  59.         mot = ROT;
  60.         motion(ox = x, oy = y);
  61.         break;
  62.     case GLUT_MIDDLE_BUTTON:
  63.         break;
  64.     }
  65.     } else if (state == GLUT_UP) {
  66.     mot = 0;
  67.     }
  68. }
  69.  
  70. static GLfloat s_plane[4] = { 1.0, 0., 0., 0.};
  71. static GLfloat t_plane[4] = { 0., 0., 1.0, 0.};
  72. static GLfloat fog_params[5] = {.015, .1, .2, .2, .1};
  73.  
  74. void init(void) {
  75.     int width, height, components;
  76.     GLubyte *image;
  77.  
  78.     glClearColor (0.0, 0.0, 0.0, 0.0);
  79.     glEnable(GL_DEPTH_TEST);
  80.     glShadeModel(GL_SMOOTH);
  81.  
  82.     if (!(image = (GLubyte *)read_texture("data/sea.rgb", &width, &height, &components))) {
  83.     perror("sea.rgb");
  84.     exit(EXIT_FAILURE);
  85.     }
  86.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  87.  
  88.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  89.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  90.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  91.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  92.     glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
  93.         GL_RGBA, GL_UNSIGNED_BYTE, image);
  94.  
  95.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  96.     glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  97.     glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
  98.     glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
  99.     glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
  100.  
  101.     glEnable(GL_TEXTURE_GEN_S);
  102.     glEnable(GL_TEXTURE_GEN_T);
  103.     glEnable(GL_TEXTURE_2D);
  104.     glEnable(GL_CULL_FACE);
  105.     glEnable(GL_LIGHTING);
  106.     glEnable(GL_LIGHT0);
  107.     glEnable(GL_AUTO_NORMAL);
  108.     glEnable(GL_NORMALIZE);
  109.     glFrontFace(GL_CW);
  110.     glCullFace(GL_BACK);
  111.     glMaterialf (GL_FRONT, GL_SHININESS, 64.0);
  112.  
  113.     glClearColor(.09f,.18f,.18f,1.f);
  114.  
  115.     glFogi(GL_FOG_MODE, GL_EXP);
  116.     glFogf(GL_FOG_DENSITY, fog_params[0]);
  117.     glFogfv(GL_FOG_COLOR, fog_params+1);
  118.     glEnable(GL_FOG);
  119.     {
  120.     GLfloat pos[] = {0.,150.,1.,1.};
  121.     glLightfv(GL_LIGHT0, GL_POSITION, pos);
  122.     }
  123. }
  124.  
  125. void display(void) {
  126.     GLfloat s, t;
  127.     static float phase;
  128.  
  129.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  130.  
  131.     glMatrixMode(GL_TEXTURE);
  132.     glPushMatrix();
  133.     s = sinf(phase); t = s; phase += M_PI/25.f;
  134.     if (phase > 2*M_PI) phase -= 2*M_PI;
  135.     glTranslatef(.5f, -0.5f, 0.f);
  136.     glScalef(.1f, .1f, 1.f);
  137.     glTranslatef(s, t, 0.f);
  138.     glMatrixMode(GL_MODELVIEW);
  139.  
  140.     glPushMatrix();
  141.     glColor4f(.09, .18, .18, 1.f);
  142.     glDisable(GL_TEXTURE_2D);
  143.     glTranslatef(0., 0., -160.);
  144.     glScalef(200., 200., 1.);
  145.     glBegin(GL_POLYGON);
  146.     glVertex3f(-1.,-1.,0.);
  147.     glVertex3f( 1.,-1.,0.);
  148.     glVertex3f( 1., 1.,0.);
  149.     glVertex3f(-1., 1.,0.);
  150.     glEnd();
  151.     glPopMatrix();
  152.     glPushMatrix();
  153.     glEnable(GL_TEXTURE_2D);
  154.     glTranslatef(0., 0., -100.+transx);
  155.     glRotatef(rotx, 0.0, 1.0, 0.0);
  156.     glScalef(10.f, 10.f, 10.f);
  157.     glutSolidTeapot(2.0);
  158.     glPopMatrix ();
  159.     glMatrixMode(GL_TEXTURE);
  160.     glPopMatrix();
  161.     glMatrixMode(GL_MODELVIEW);
  162.     glutSwapBuffers();
  163. }
  164.  
  165. void reshape(int w, int h) {
  166.     glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  167.     glMatrixMode(GL_PROJECTION);
  168.     glLoadIdentity();
  169.     gluPerspective(40., 1.0, 10.0, 200000.);
  170.     glMatrixMode(GL_MODELVIEW);
  171.     glLoadIdentity();
  172. }
  173.  
  174. void ffunc(void) {
  175.     static int state = 1;
  176.     if (state ^= 1)
  177.     glEnable(GL_FOG);
  178.     else
  179.     glDisable(GL_FOG);
  180. }
  181.  
  182. void help(void) {
  183.     printf("Usage: smoke [image]\n");
  184.     printf("'h'           - help\n");
  185.     printf("'f'           - toggle fog\n");
  186.     printf("left mouse    - pan\n");
  187.     printf("right mouse   - rotate\n");
  188. }
  189.  
  190. /*ARGSUSED1*/
  191. void key (unsigned char key, int x, int y) {
  192.    switch (key) {
  193.       case 'f': ffunc(); break;
  194.       case 'h': help(); break;
  195.       case '\033': exit(EXIT_SUCCESS); break;
  196.       default: break;
  197.    }
  198. }
  199.  
  200. void animate(void) {
  201.     glutPostRedisplay();
  202. }
  203.  
  204. int main(int argc, char** argv) {
  205.     glutInitWindowSize(256, 256);
  206.     glutInit(&argc, argv);
  207.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  208.     glutInitWindowPosition(100, 100);
  209.     glutCreateWindow (argv[0]);
  210.     init();
  211.     glutDisplayFunc(display);
  212.     glutReshapeFunc(reshape);
  213.     glutKeyboardFunc(key);
  214.     glutIdleFunc(animate);
  215.     glutMouseFunc(mouse);
  216.     glutMotionFunc(motion);
  217.  
  218.     glutMainLoop();
  219.     return 0;
  220. }
  221.